﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ADO_QLNS.Utils
{
    public class ConfigReader
    {
        private XmlDocument xmlDocument;
        private XmlTextReader xmlTextReader;

        public void InitReader(string FilePath)
        {
            xmlDocument = new XmlDocument();
            xmlTextReader = new XmlTextReader(FilePath);
        }

        public void LoadConfig()
        {
            // Load all content of xml file
            xmlDocument.Load(xmlTextReader);

            // Get to root node of xml file
            XmlElement xmlElement = xmlDocument.DocumentElement;
            // Get tag name of root node
            string type = xmlElement.Name;
            // Register tag name as configuration type to ConfigurationManager
            ConfigurationManager.RegisterType(type);

            // Read config value
            foreach (XmlNode xmlNode in xmlElement.ChildNodes)
            {
                // Read config name
                string name = xmlNode.Attributes["name"].Value;
                // Read config value
                string value = xmlNode.Attributes["connectionString"].Value;
                // Register config setting
                ConfigurationManager.RegisterValue(type, name, value);
            }
        }
    }
}
